import axios from 'axios'

// 获取用户分页数据
export async function fetchUsers (params) {
  const { page, pageSize, roleType, username } = params
  const loginResponseStr = localStorage.getItem('loginResponse')
  if (!loginResponseStr) {
    throw new Error('用户未登录，请先登录')
  }
  const loginResponse = JSON.parse(loginResponseStr)
  if (!loginResponse?.data?.token) {
    throw new Error('登录信息无效，请重新登录')
  }
  const response = await axios.get('http://localhost:8080/admin/user/page', {
    params: {
      page,
      pageSize,
      role: roleType,
      username: username || undefined // 如果 username 为空，则不传递该参数
    },
    headers: {
      Accept: 'application/json',
      token: loginResponse.data.token
    }
  })
  console.log(response)
  if (!response.data?.data?.records) {
    throw new Error('后端返回数据格式不正确')
  }
  return response.data.data // 返回整个 response.data.data
}

export async function createUser (user) {
  const loginResponseStr = localStorage.getItem('loginResponse')
  const loginResponse = JSON.parse(loginResponseStr)
  try {
    const response = await axios.post('http://localhost:8080/admin/user/add', user, {
      headers: {
        'Content-Type': 'application/json',
        token: loginResponse.data.token
      }
    })
    return response.data
  } catch (error) {
    console.error('创建用户失败:', error)
    throw error
  }
}

// 更新用户状态
export const ChangeStaucs = async (id, status, updateDataList) => {
  console.log(status)
  const loginResponseStr = localStorage.getItem('loginResponse')
  const loginResponse = JSON.parse(loginResponseStr)
  console.log(loginResponse.data.token)
  try {
    const response = await axios.post(`http://localhost:8080/admin/user/${id}`, { status: status }, {
      headers: {
        'Content-Type': 'multipart/form-data',
        token: loginResponse.data.token
      }
    })
    updateDataList(response.data) // 通过回调函数更新数据
  } catch (error) {
    console.error('更新状态失败:', error)
  }
}

// 获取会议室分页数据
export async function fetchMeetingRooms (params) {
  const { page, pageSize, roomName } = params
  const loginResponseStr = localStorage.getItem('loginResponse')
  if (!loginResponseStr) {
    throw new Error('用户未登录，请先登录')
  }
  const loginResponse = JSON.parse(loginResponseStr)
  if (!loginResponse?.data?.token) {
    throw new Error('登录信息无效，请重新登录')
  }
  const response = await axios.get('http://localhost:8080/admin/meetingroom/page', {
    params: {
      page,
      pageSize,
      roomName: roomName || undefined // 如果 roomName 为空，则不传递该参数
    },
    headers: {
      Accept: 'application/json',
      token: loginResponse.data.token
    }
  })
  console.log(response)
  if (!response.data?.data?.records) {
    throw new Error('后端返回数据格式不正确')
  }
  console.log(response.data)
  return response.data.data // 返回整个 response.data.data
}

// 更新用户信息
export async function updateInform (data) {
  const loginResponseStr = localStorage.getItem('loginResponse')
  const loginResponse = JSON.parse(loginResponseStr)
  try {
    const response = await axios.put('http://localhost:8080/admin/user/update', data, {
      headers: {
        'Content-Type': 'application/json',
        token: loginResponse.data.token
      }
    })
    console.log(response.data) // 打印响应数据
  } catch (error) {
    console.error('更新失败:', error)
    throw error
  }
}

export async function DeleteUser (id) {
  const loginResponseStr = localStorage.getItem('loginResponse')
  const loginResponse = JSON.parse(loginResponseStr)
  try {
    const response = await axios.delete(`http://localhost:8080/admin/user/delete/${id}`, {
      headers: {
        'Content-Type': 'application/json',
        token: loginResponse.data.token
      }
    })
    return response
  } catch (error) {
    console.error('删除失败:', error)
    throw error
  }
}

export async function createRoom (data) {
  const loginResponseStr = localStorage.getItem('loginResponse')
  const loginResponse = JSON.parse(loginResponseStr)
  try {
    const response = await axios.post('http://localhost:8080/admin/meetingroom/add', data, {
      headers: {
        'Content-Type': 'application/json',
        token: loginResponse.data.token
      }
    })
    return response.data
  } catch (error) {
    console.error('创建会议室失败:请填写完整信息')
    throw error
  }
}

// 更新会议室
export async function updateRoom (data) {
  const loginResponseStr = localStorage.getItem('loginResponse')
  const loginResponse = JSON.parse(loginResponseStr)
  try {
    const response = await axios.put('http://localhost:8080/admin/meetingroom/update', data, {
      headers: {
        'Content-Type': 'application/json',
        token: loginResponse.data.token
      }
    })
    console.log(response.data) // 打印响应数据
  } catch (error) {
    console.error('更新失败:', error)
    throw error
  }
}

export async function DeleteRoom (id) {
  const loginResponseStr = localStorage.getItem('loginResponse')
  const loginResponse = JSON.parse(loginResponseStr)
  try {
    const response = await axios.delete(`http://localhost:8080/admin/meetingroom/delete/${id}`, {
      headers: {
        'Content-Type': 'application/json',
        token: loginResponse.data.token
      }
    })
    return response
  } catch (error) {
    console.error('删除失败:', error)
    throw error
  }
}
